var defaultEmptyOK = true;

function IsPassWord(obj) {
	var tmp = obj.value;
	if (isWhitespace(tmp)) {
		return false;
	}
	var strTmp = new String(tmp);
	var str1 = "0123456789";
	var str2 = "abcdefghijklmnopqrstuvwxyz";
	var str3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var idx1 = -1;
	var idx2 = -1;
	var idx3 = -1;
	for (i = 0; i < strTmp.length; i = i + 1) {
		if (str1.indexOf(strTmp.charAt(i)) > -1) {
			idx1 = 0;
		}
		if (str2.indexOf(strTmp.charAt(i)) > -1) {
			idx2 = 0;
		}
		if (str3.indexOf(strTmp.charAt(i)) > -1) {
			idx3 = 0;
		}
	}
	if (idx1 < 0 || idx2 < 0 || idx3 < 0) {
		return false;
	}
	return true;



}

function isEmptyToo(s) {
	return ((s == null) || (s.length == 0));
}

function isWhitespace(s) {
	var whitespace = " \t\n\r";
	var i;
	if (isEmptyToo(s)) {
		return true;
	}
	for (i = 0; i < s.length; i = i + 1) {
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) {
			return false;
		}
	}
	return true;
}

function strlen(str) {
	var len;
	var i;
	len = 0;
	for (i = 0; i < str.length; i = i + 1) {
		if (str.charCodeAt(i) > 255) {
			len += 2;
		} else {
			len = len + 1;
		}
	}
	return len;
}

function LTrim(str) {
	if (str.charAt(0) == "　" || str.charAt(0) == " ") {
		str = str.slice(1);
		str = LTrim(str); 
	}

	return str;
}

function RTrim(str) {
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length - 1)) != -1) {
		var i = s.length - 1;
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) {
			i = i - 1;
		}
		s = s.substring(0, i + 1);
	}
	return s;
}

function Trim(str) {
	return RTrim(LTrim(str));
}

function LTrimZero(str) {
	var whitespace = new String("0");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1) {
		var j = 0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1) {
			j = j + 1;
		}
		s = s.substring(j, i);
	}
	return s;
}

window.onbeforeunload = disabledButton;

function disabledButton() {
	if (startProgress()) {
		return;
	}
	for (var m = 0; m < document.forms.length; m++) {
		for (var i = 0; i < document.forms[m].elements.length; i++) {
			var e = document.forms[m].elements[i];
			if (e.type == "button") {
				e.disabled = true;
			}
			if (e.type == "submit") {
				e.disabled = true;
			}
			if (e.type == "reset") {
				e.disabled = true;
			}
		}
	}
}

function enabledButton() {
	if (stopProgress()) {
		return;
	}
	for (var m = 0; m < document.forms.length; m++) {
		for (var i = 0; i < document.forms[m].elements.length; i++) {
			var e = document.forms[m].elements[i];
			if (e.type == "button") {
				e.disabled = false;
			}
			if (e.type == "submit") {
				e.disabled = false;
			}
			if (e.type == "reset") {
				e.disabled = false;
			}
		}
	}
}

function isRenderedMessagePanel() {
	if (document.all('messagePanel') == undefined) {
		return false;
	} else {
		return true;
	}

}

function mOverBtn(obj) {
	obj.className = 'MoveOverBtn';
}

function mOutBtn(obj) {
	obj.className = 'MoveOutBtn';
}

function isInteger(s)
{
	var i;

	if (isEmptyToo(s))
		if (isInteger.arguments.length == 1)
			return defaultEmptyOK;
		else
			return (isInteger.arguments[1] == true);

	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!isDigit(c))
			return false;
	}

	return true;
}
function isDigit(c) {
	return ((c >= "0") && (c <= "9"))
}

function checkPhoneNumber(strTel) {
	for (var i = 0; i < strTel.length; i++) {
		var ch = strTel.charAt(i);
		if ((ch >= '0' && ch <= '9') || ch == '-' || ch == '(' || ch == ')'
				|| ch == '+' || ch == ' ') {
			continue;
		} else {
			return false;
		}
	}
	return true;
}


String.prototype.isEmail = function() {
	return /^\w+@.+\.\w+$/g.test(this);
}

function emailVerify(field) {
	if (field.isEmail()) {
		return true;
	}
	return false;
}

function isChinese(str) {

	var chinese = /[^\x00-\xff]/g;
	if (chinese.test(str)) {
		return true;
	}
	return false;
}

function doNavigete(obj) {
	document.getElementById('topForm:currentMenuId').value = obj;
	document.getElementById('topForm:navbutton').click();
	var o = document.getElementById("topForm:menuid-" + obj);
	var arya = document.getElementsByTagName("a");
	for (var i = 0; i < arya.length; i++) {
		var id = arya[i].id;
		if (id.indexOf("topForm:menuid-") >= 0) {
			arya[i].style.color = "#FFFFFF";
		}
	}
	o.style.color = "#0e267a";
}

function treeNodeChecked(nodeBox, formName) {

	var boxID = nodeBox.name.split("::");
	var prefixName = boxID[0];
	var postfixName = boxID[1];
	var formObject = document.getElementById(formName);

	for (var i = 0; i < formObject.elements.length; i++) {

		var subNode = formObject.elements[i];
		if (subNode.name.indexOf("::" + postfixName) == -1) {
			continue;
		}
		if (subNode.name == nodeBox.name
				|| subNode.name.indexOf(prefixName + ":") == -1) {
			continue;
		}
		subNode.checked = nodeBox.checked;
	}

	var tmpBoxID = nodeBox.name.split("::");
	var tmp = tmpBoxID[0];
	tmp = tmp.substring(0, tmp.lastIndexOf(":"));
	tmp = tmp.substring(0, tmp.lastIndexOf(":"));

	while (tmp.length != 0) {
		var superNodePrefixName = tmp;
		var superNodePostfixName = tmpBoxID[1];
		var superNodeName = superNodePrefixName + "::" + superNodePostfixName;
		var superNode = document.getElementById(superNodeName);
		if (superNode == undefined) {
			return;
		}
		var equCount = 0;
		var equCheckedCount = 0;
		for (var j = 0; j < formObject.elements.length; j++) {
			var equNode = formObject.elements[j];
			if (equNode.name.indexOf("::" + superNodePostfixName) == -1) {
				continue;
			}
			if (equNode.name == superNodeName
					|| equNode.name.indexOf(superNodePrefixName + ":") == -1) {
				continue;
			}
			equCount++;
			if (equNode.checked) {
				equCheckedCount++;
			}
		}
		if (equCount > 0 && equCount == equCheckedCount) {
			superNode.checked = true;
		} else {
			superNode.checked = false;
		}
		tmp = tmp.substring(0, tmp.lastIndexOf(":"));
		tmp = tmp.substring(0, tmp.lastIndexOf(":"));
	}
}

function popRecursiveTree(collName, idName, showName, parentName, completeBtn,
		readonly, radio, checkedCollName) {
	popRecursiveSelectTree(collName, idName, showName, parentName, completeBtn,
			readonly, radio, checkedCollName);
}

function popRecursiveTreeCheckOne(collName, idName, showName, parentName,
		completeBtn, readonly, radio, checkedCollName) {
	popRecursiveSelectTree(collName, idName, showName, parentName, completeBtn,
			readonly, radio, checkedCollName, true);
}

function popRecursiveSelectTree(collName, idName, showName, parentName,
		completeBtn, readonly, radio, checkedCollName, checkone) {
	var width = 548;
	var height = 400;
	var param = "?collName=" + collName + "&idName=" + idName + "&showName="
			+ showName + "&parentName=" + parentName + "&completeBtn="
			+ completeBtn + "&readonly=" + readonly + "&radio=" + radio
			+ "&initTree=true";
	if (checkedCollName != undefined) {
		param = param + "&checkedCollName=" + checkedCollName;
	}
	if (checkone != undefined && checkone) {
		param = param + "&checkone=true";
	} else {
		param = param + "&checkone=false";
	}
	var newin = window
			.open(
					'',
					'PopupTree',
					'width=0, height=0, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, top='
							+ screen.height + ',left=' + screen.width);
	newin.close();
	var pagePrefix = document.location.href.substring(0, document.location.href
					.indexOf("pages"));
	window
			.open(
					pagePrefix + 'pages/richcommon/popupRecursiveTree.jsf'
							+ param,
					'PopupTree',
					'width='
							+ width
							+ ', height='
							+ height
							+ ', top='
							+ (screen.height - height)
							/ 2
							+ ',left='
							+ (screen.width - width)
							/ 2
							+ ', toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no');
}

function fspTurnPage(maxPage, inputValue, dataScroll) {
	var s;
	if (document.getElementById(inputValue)) {
		s = document.getElementById(inputValue).value;
	} else {
		s = document.getElementsByName(inputValue)[0].value
	}
	if (isEmptyToo(s) || !isInteger(s)) {
		alert('\u8bf7\u5f55\u5165\u8df3\u8f6c\u9875\u9762');
		return false;
	}
	if (s > maxPage) {
		alert('\u8df3\u8f6c\u9875\u9762\u4e0d\u80fd\u5927\u4e8e' + maxPage);
		return false;
	}
	if (s < 1) {
		alert('\u8df3\u8f6c\u9875\u9762\u4e0d\u80fd\u5c0f\u4e8e1');
		return false;
	}
	document.getElementById(dataScroll).component.switchToPage(s);
}

function mOverBtn_Other(obj, btn_type) {
	obj.className = 'MoveOverBtn_' + btn_type;
}

function mOutBtn_Other(obj, btn_type) {
	obj.className = 'btn_' + btn_type;
}


function refreshCtx() {
	var s1 = location.pathname;
	var s2 = s1.split("/");
	var rfCtxWin = window
			.open(
					s2[0] + "/" + s2[1] + "/" + 'refresh',
					'rfCtxWinMdef',
					'width=0, height=0, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, top='
							+ screen.height + ',left=' + screen.width);
	rfCtxWin.close();
	//刷新配置文件完成。
	alert('\u5237\u65b0\u914d\u7f6e\u6587\u4ef6\u5b8c\u6210\u3002');
}
var progressCounter = 1;
var progressIntervalID;
var progressBarID = "progressBarForm:progressBar";
var useParentProgressBar = (window.parent.document.all(progressBarID) != undefined);
function startProgress() {
	try {
		if (document.all(progressBarID) == undefined) {
			return false;
		}
		progressCounter = 1;
		if (useParentProgressBar) {
			window.parent.document.all(progressBarID).component.setValue(3);
			window.parent.document.all(progressBarID).component.enable();
			window.parent.Richfaces.showModalPanel('progressBarModal');
			progressIntervalID = window.parent.setInterval(updateProgress, 100);
		} else {
			document.all(progressBarID).component.setValue(3);
			document.all(progressBarID).component.enable();
			Richfaces.showModalPanel('progressBarModal');
			progressIntervalID = setInterval(updateProgress, 100);
		}
		return true;
	} catch (e) {
		return false;
	}
}
function updateProgress() {
	if (useParentProgressBar) {
		window.parent.document.all(progressBarID).component
				.setValue(progressCounter * 5);
	} else {
		document.all(progressBarID).component.setValue(progressCounter * 5);
	}
	if ((progressCounter++) > 20) {
		if (useParentProgressBar) {
			window.parent.clearInterval(progressIntervalID);
		} else {
			clearInterval(progressIntervalID);
		}
		startProgress();
	}
}
function stopProgress() {
	
	try {
		if (document.all(progressBarID) == undefined) {
			return false;
		}
		if (useParentProgressBar) {
			window.parent.clearInterval(progressIntervalID);
			window.parent.Richfaces.hideModalPanel('progressBarModal');
			window.parent.document.all(progressBarID).component.disable();
		} else {
			clearInterval(progressIntervalID);
			Richfaces.hideModalPanel('progressBarModal');
			document.all(progressBarID).component.disable();
		}
		return true;
	} catch (e) {
		return false;
	}

}

var _cleanID;
function cleanProgress(btnID, cleanTime) {
	_cleanID = setTimeout("stopProgress()", cleanTime);
	if (btnID != undefined) {
		document.all(btnID).click();
	}
}

window.onhelp = function() {
	return false; 
}

document.onkeydown = function() {
	if ((window.event.altKey) && ((window.event.keyCode == 37) || 
																	
			(window.event.keyCode == 39) || 
			window.event.keyCode == 13)) 
	{
		event.returnValue = false;
		return false;
	}
	if (
	(event.keyCode == 8 && (event.srcElement == null
			|| (event.srcElement.type != "text"
					&& event.srcElement.type != "textarea" && event.srcElement.type != "password") || isElReadOnly(event.srcElement)))
			|| (event.keyCode == 116) || 
			(event.ctrlKey && event.keyCode == 82)) { 
		event.keyCode = 0;
		event.returnValue = false;
	}
	if (event.keyCode == 122) {
		event.keyCode = 0;
		event.returnValue = false;
	} 
	if (event.ctrlKey && event.keyCode == 78)
		event.returnValue = false; 
	if (event.shiftKey && event.keyCode == 121)
		event.returnValue = false; 
	if (window.event.srcElement.tagName == "A" && window.event.shiftKey)
		window.event.returnValue = false; 
	if ((window.event.altKey) && (window.event.keyCode == 115)) 
	{
		window.showModelessDialog("about:blank", "",
				"dialogWidth:1px;dialogheight:1px");
		return false;
	}
}

function isElReadOnly(obj) {
	if (obj.readOnly == 'readOnly' || obj.readOnly == 'yes'
			|| obj.readOnly == true || obj.readOnly == 'true'
			|| obj.disabled == 'disabled' || obj.disabled == true
			|| obj.disabled == 'yes') {
		return true;
	} else {
		return false;
	}

}

function mOverTableRow(obj) {
	obj.className = 'MouseOverRow';
}

function mOutTableRow(obj) {
	obj.className = 'MouseOutRow';
}

function mOverTableHead(obj) {
	obj.className = 'ColumnSortIn';
}

function mOutTableHead(obj) {
	obj.className = 'ColumnSortOut';
}

function disabledClassesProv(day) {
	var curDt = new Date();
	if (curDt.getTime() - day.date.getTime() >= 0) {
		return '';
	} else {
		return 'disableAheadDay'
	}
}
function cleanProgressExcel(btnID,cleanTime){
   _cleanID = setTimeout("_cleanProgressBarExcel('"+btnID+"')",cleanTime);
}
function _cleanProgressBarExcel(btnID){
    stopProgress();
    if(_cleanID != undefined){
        clearTimeout(_cleanID);
    }
    if(btnID != undefined){
        document.all(btnID).click();
    }
}

/**
 * 检查指定表单的rich:fileUpload是否存在未上传的文件对象
 * 返回false，如果存在未上传的文件
 * 调用方法
 *  checkRichFileUploadEntryState("formID:fileUploadId","formID:fileUploadId2","formID:fileUploadId3"[,"formID:fileUploadId"])
 */
function checkRichFileUploadEntryState(){
	var args = arguments;//获取所有实参
	for (var i=0; i<args.length; i++){
		var fileUploadCompId = args[i];
		var fileUpload = $(fileUploadCompId) || document.getElementById(fileUploadCompId);
		if (fileUpload == null || fileUpload == undefined) continue;//如果没有找到则跳过
		//fileUpload.component.upload();
		var entries = fileUpload.component.entries; //当前被找到的文件上传组件的下所有选择的条目
		if (entries.length == 0) continue; //如果没有选择 跳过
		for (var j=0; j<entries.length; j++){
			var entry = entries[j];
			if (entry.state == FileUploadEntry.INITIALIZED){//未上传状态
				alert("文件上传组件:"+fileUploadCompId+"\n\n存在未上传的附件, 请先上传所选择的附件");
				return false;
			}
		}
	}
	return true;
}